package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import model.Cliente;
import model.Endereco;


public class ClienteDAO {
	
	Connection conn;
	PreparedStatement pstm;
	String query;
	
	public boolean inclusaoFisica(Cliente t, Endereco e) {
	
		  query = "INSERT INTO dbo.CLIENTE_F ( NOME_CLIENTE, CPF, RG, EMAIL, ENDERECO, NUMERO, COMPLEMENTO, BAIRRO, CEP, CIDADE, ESTADO, TELEFONE_1, TELEFONE_2 )"+ 
		           "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		 
		  try {
			    conn = Conexao.getConexao();
				pstm = conn.prepareStatement(query);
			
				pstm.setString(1, t.getNome());    
				pstm.setString(2, t.getCpf());
				pstm.setString(3, t.getRg());
				pstm.setString(4, t.getEmail());    
				pstm.setString(5, e.getLogradouro());
				pstm.setString(6, e.getNumero());
				pstm.setString(7, e.getComplemento());
				pstm.setString(8, e.getBairro());
				pstm.setString(9, e.getCep());
				pstm.setString(10, e.getCidade());
				pstm.setString(11, e.getEstado());
				pstm.setString(12, e.getTelefone1());
				pstm.setString(13, e.getTelefone2());
				
				if(pstm.executeUpdate() > 0){
	                pstm.close();
	                return true;
	            }else{
	                pstm.close();
	                System.err.println("Erro ao tentar incluir cliente_F no banco ");
	                return false;
	            }
		  }catch(Exception f){
				System.err.println("Erro ao tentar incluir cliente_F no banco "+ f);
				return false;
			}		
			  			
 }
	
	public boolean inclusaoJuridica(Cliente t, Endereco e){
		
		 query = "INSERT INTO dbo.CLIENTE_J (RAZAOSOCIAL, CNPJ, IE, IM, CONTATO, EMAIL, ENDERECO, NUMERO, COMPLEMENTO, BAIRRO, CEP, CIDADE, ESTADO, TELEFONE_1, TELEFONE_2 )"+ 
			        "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		 int i=1;	  
		  try {
			    conn = Conexao.getConexao();
				pstm = conn.prepareStatement(query);
			
				pstm.setString(1, t.getRazaoSocial());    
				pstm.setString(2, t.getCnpj());
				pstm.setString(3, t.getInscricaoEstadual()); 
				pstm.setString(4, t.getInscricaoMunicipal()); 
				pstm.setString(5, t.getNomeContato()); 
				pstm.setString(6, t.getEmail());    
				pstm.setString(7, e.getLogradouro());
				pstm.setString(8, e.getNumero());
				pstm.setString(9, e.getComplemento());
				pstm.setString(10, e.getBairro());
				pstm.setString(11, e.getCep());
				pstm.setString(12, e.getCidade());
				pstm.setString(13, e.getEstado());
				pstm.setString(14, e.getTelefone1());
				pstm.setString(15, e.getTelefone2());		
				
				if(pstm.executeUpdate() > 0){
	                pstm.close();
	                return true;
	            }else{
	                pstm.close();
	                System.err.println("Erro ao tentar incluir cliente_J no banco ");
	                return false;
	            }
		  }catch(Exception f){
				System.err.println("Erro ao tentar incluir cliente_J no banco "+ f);
				return false;
		        }	
		
		
	}
	

	public boolean alteracao() {
		// TODO Auto-generated method stub
		return false;
	}

	
	public boolean exclusao() {
		// TODO Auto-generated method stub
		return false;
	}


	public boolean busca() {
		
		// TESTE DE CONEXAO BUSCA
		
		Connection conn;
		
		ResultSet rs;
		
		PreparedStatement pstm;
		
		String query = "SELECT * FROM AuditLog";
		
		conn = Conexao.getConexao();
		
		try {
			
			pstm = conn.prepareStatement(query);

			
			rs = pstm.executeQuery();
			
			while(rs.next()){
				
				System.out.println(rs.getString("descricao"));
									
			}
		return true;
	}catch(Exception e){
		System.err.println(e);
		return false;
	}
	
	
	}
	
}


